package com.teatime.client.activity;

import com.teatime.client.Constants;
import com.teatime.client.R;
import com.teatime.client.TLog;
import com.teatime.client.TeaTimeApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public abstract class TActivity extends Activity {

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getActitityMgr().push(this);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getContentViewId());
	}
	
	public abstract int getContentViewId();
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tea_time, menu);
        return true;
    }
	
	protected Intent activityIntent(String componentName) {
        Intent result = new Intent();
        if (componentName.startsWith(".")) {
        	componentName = Constants.packageName + componentName;
        }
        TLog.v("activityIntent: package: " + Constants.packageName +
        		", componentName: " + componentName);
        result.setClassName(Constants.packageName, componentName);
        return result;
    }
    
    public void startActivity(String componentName) {
    	startActivity(componentName, 0);
    }
    
    public void startActivity(String componentName, int flags) {
    	Intent i = activityIntent(componentName);
    	if (flags != 0) {
    		i.setFlags(flags);
    	}
    	startActivity(i);
    }
    
    public String getTextFromEdit(int editId) {
    	String ret = null;
    	EditText txt = (EditText)this.findViewById(editId);
    	if (txt != null) {
    		ret = txt.getText().toString();
    	}
    	return ret;
    }
    
    public void showErrMsg(int id) {
    	Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
    }
    
    public void showErrMsg(String msg) {
    	Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
    
    public TeaTimeApp getApp() {
    	return (TeaTimeApp)getApplication();
    }
    
    public TActivityManager getActitityMgr() {
    	return getApp().getActivityManager();
    }
    
    public void finishActual() {
    	super.finish();
    }
    
    /*public void finish() {
    	TLog.v("TActivity.finish");
    	getActitityMgr().popIf(this);
    	finishActual();
    }*/
}
